using System.Collections;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Tilemaps;

public class Bomb : MonoBehaviour
{
    public static Bomb instance;
    [Header("炸弹")]
    private KeyCode inputKey = KeyCode.Space;  // 输入的按键
    public GameObject bombPrefab;  // 炸弹预制体
    public float bombFuseTime = 3f;  // 炸弹引线时间


    [Header("爆炸")]
    private Tilemap wallTileMap; // 可破坏物墙壁的Tilemap组件
    public GameObject explosionEnd; // 爆炸结束
    public GameObject explosionMiddle; // 爆炸中间
    public GameObject explosionStart; // 爆炸结束
    public GameObject brickWall;//破坏的墙

    public LayerMask explosionLayerMask;  // 爆炸层级掩码
    
    public Transform point;//炸弹生成点

    public bool setBomb = false;
    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            if (instance != this)
            {
                Destroy(gameObject);
            }
        }
    }
    void Start()
    {
        wallTileMap = GameObject.Find("墙壁").GetComponent<Tilemap>();
    }
    private void OnEnable()
    {
        GameApp.instance.bombsRemaining = GameApp.instance.bombAmount;  // 初始化剩余炸弹数量
    }

    private void Update()
    {
        if (GameApp.instance.bombsRemaining > 0)  // 如果还有剩余炸弹且按下了指定按键
        {
            if (Input.GetKeyDown(inputKey) || setBomb)
            {
                setBomb = false;
                //播放放置音效
                AudioManager.Instance.PlaSound("Fire");
                StartCoroutine(PlaceBomb());  // 放置炸弹
            }
        }
    }

    private IEnumerator PlaceBomb()
    {
        Vector2 position = point.position;  // 获取当前位置
        position.x = Mathf.Round(position.x)- 0.5f;  // 四舍五入x坐标-0.5偏移量
        position.y = Mathf.Round(position.y) - 0.5f;  // 四舍五入y坐标-0.5偏移量

        GameObject bomb = Instantiate(bombPrefab, position, Quaternion.identity);  // 实例化炸弹
        GameApp.instance.bombsRemaining--;  // 剩余炸弹数量减一
        yield return new WaitForSeconds(bombFuseTime);  // 等待炸弹引线时间
        Destroy(bomb);  // 销毁炸弹游戏对象
        GameApp.instance.bombsRemaining++;  // 剩余炸弹数量加一


        //播放爆炸音效
        AudioManager.Instance.PlaSound("Boom");
        createExplosion(bomb.transform.position);
    }

    //生成爆炸效果
    public void createExplosion(Vector2 position)
    {
        //爆炸中心
        GameObject explosionStartData = Instantiate(explosionStart, position, Quaternion.identity);
        Destroy(explosionStartData, 0.5f);

        for (int i = 1; i <= GameApp.instance.explosionRange; i++)
        {
            bool res = ClearDestructible(new Vector2(position.x + i, position.y), i, 0);
            if (!res) break;
        }
        for (int i = 1; i <= GameApp.instance.explosionRange; i++)
        {
            bool res = ClearDestructible(new Vector2(position.x - i, position.y), i, 180);
            if (!res) break;
        }
        for (int i = 1; i <= GameApp.instance.explosionRange; i++)
        {
            bool res = ClearDestructible(new Vector2(position.x, position.y + i), i, 90);
            if (!res) break;
        }
        for (int i = 1; i <= GameApp.instance.explosionRange; i++)
        {
            bool res = ClearDestructible(new Vector2(position.x, position.y - i), i, -90);
            if (!res) break;
        }

    }

    private bool ClearDestructible(Vector2 position, int i, int rotate)
    {
        if (Physics2D.OverlapBox(position, new Vector2(0.5f, 0.5f), 0f, explosionLayerMask))  // 如果爆炸位置有墙壁
        {
            return false;
        }

        Vector3Int cell = wallTileMap.WorldToCell(position);  // 将世界坐标转换为Tilemap的单元格坐标
        TileBase tile = wallTileMap.GetTile(cell);  // 获取指定单元格的Tile
        if (tile != null)  // 如果爆炸位置有可破坏障碍物
        {
            wallTileMap.SetTile(cell, null);  // 清除Tile

            Instantiate(brickWall, position, Quaternion.identity);  // 实例化破坏的墙

            return false;
        }
        else
        {
            //是不是最后爆炸区
            if (i == GameApp.instance.explosionRange)
            {
                GameObject explosionEndData = Instantiate(explosionEnd, position, Quaternion.identity);
                //设置爆炸效果的方向
                explosionEndData.transform.eulerAngles = new Vector3(0, 0, rotate);

                Destroy(explosionEndData, 0.5f);
            }
            else
            {
                GameObject explosionMiddleData = Instantiate(explosionMiddle, position, Quaternion.identity);
                //设置爆炸效果的方向
                explosionMiddleData.transform.eulerAngles = new Vector3(0, 0, rotate);
                Destroy(explosionMiddleData, 0.5f);
            }
            return true;
        }
    }

    //角色离开炸弹时开启碰撞效果
    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.layer == LayerMask.NameToLayer("Bomb"))  // 如果离开触发器的物体属于Bomb层
        {
            other.isTrigger = false;  // 取消触发器属性
        }
    }

}
